import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/pages/Login'
import Index from '@/components/pages/Index'

import Product from '@/components/pages/product/Index'
import Banner from '@/components/pages/banner/Index'
import AddProduct from '@/components/pages/product/Add'
import Welcome from '@/components/pages/FirstPage'
import Order from '@/components/pages/order/Index'
import Withdraw from '@/components/pages/withdraw/Index'
import Ticket from '@/components/pages/ticket/Index'
import HasPayOrder from '@/components/pages/report/HasPayOrder'
import AddTicket from '@/components/pages/ticket/Add'
import Activity from '@/components/pages/activity/Index'
import AddActivity from '@/components/pages/activity/Add'
import User from '@/components/pages/user/Index'
import Classification from '@/components/pages/classification/Index'
import UserDetail from '@/components/pages/user/UserDetail'
import OrderDetail from '@/components/pages/order/OrderDetail'

Vue.use(Router)

const RouterConfig = new Router({
  // mode: 'history',
  routes: [
    {
      path: '/',
      redirect: '/login'
    },
    { // 登录
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/index',
      name: 'Index',
      redirect: '/index/welcome',
      component: Index,
      children: [
        { // 欢迎页
          name: 'welcome',
          path: 'welcome',
          component: Welcome
        },
        { // banner管理
          name: 'banner',
          path: 'banner',
          component: Banner
        },
        { // 分类管理
          name: 'classification',
          path: 'classification',
          component: Classification
        },
        { // 商品管理
          name: 'product',
          path: 'product',
          component: Product
        },
        { // 商品添加
          name: 'product_add',
          path: 'product/add',
          component: AddProduct
        },
        { // 商品编辑
          name: 'product_edit',
          path: 'product/edit/:editItemId',
          component: AddProduct
        },
        { // 订单管理
          name: 'order',
          path: 'order',
          component: Order
        },
        { // 订单详情
          path: 'order/detail/:id',
          name: 'order_detail',
          component: OrderDetail
        },
        { // 提现管理
          name: 'withdraw',
          path: 'withdraw',
          component: Withdraw
        },
        { // 奖券管理
          name: 'ticket',
          path: 'ticket',
          component: Ticket
        },
        { // 新增奖券
          name: 'ticket_add',
          path: 'ticket/add',
          component: AddTicket
        },
        { // 活动管理
          name: 'activity',
          path: 'activity',
          component: Activity
        },
        { // 活动管理
          name: 'activity_add',
          path: 'activity/add',
          component: AddActivity
        },
        { // 用户管理
          path: 'user',
          name: 'user',
          component: User
        },
        { // 用户详情
          path: 'user/:id/detail',
          name: 'user_detail',
          component: UserDetail
        },
        { // 统计报表
          path: 'has_pay_report',
          name: 'has_pay_report',
          component: HasPayOrder
        }
      ]
    }
  ]
})

// 全局路由守卫
RouterConfig.beforeEach((to, from, next) => {
  if (to.name === 'Login') {
    next()
  }
  const accessToken = window.sessionStorage.getItem('accessToken')
  const refreshToken = window.sessionStorage.getItem('refreshToken')
  const userInfo = window.sessionStorage.getItem('userInfo')
  if (!accessToken || !refreshToken || !userInfo) {
    next({ name: 'Login' })
  } else {
    next()
  }
})

export default RouterConfig
